package com.sugon.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sugon.entity.MaintainPlanChangeEntity;
import com.sugon.entity.MaintainPlanEntity;
import com.sugon.service.MaintainPlanService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 维护计划Controller
 *
 * @author sugon
 * @date 2019-08-07 14:02:42
 */
@Controller
@RequestMapping("maintainplan")
public class MaintainPlanController {
    @Autowired
    private MaintainPlanService maintainPlanService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("maintainplan:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<MaintainPlanEntity> maintainPlanList = maintainPlanService.queryList(query);
        int total = maintainPlanService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(maintainPlanList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("maintainplan:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        MaintainPlanEntity maintainPlan = maintainPlanService.queryObject(id);

        return R.ok().put("maintainPlan", maintainPlan);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("maintainplan:save")
    @ResponseBody
    public R save(@RequestBody MaintainPlanEntity maintainPlan) {
        maintainPlanService.save(maintainPlan);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("maintainplan:update")
    @ResponseBody
    public R update(@RequestBody MaintainPlanEntity maintainPlan) {
        maintainPlanService.update(maintainPlan);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("maintainplan:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        maintainPlanService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MaintainPlanEntity> list = maintainPlanService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping(value = "/inMaintain", method = RequestMethod.POST)
    @ResponseBody
    public R inMaintain(MaintainPlanEntity maintainPlanEntity, HttpServletRequest request) throws Exception {
        String devId = request.getParameter("devId");
        Integer flag = maintainPlanService.insertMaintain(maintainPlanEntity, devId);
        if (flag > 0) {
            return R.ok("添加任务成功");
        }
        return R.error(500, "添加任务失败");
    }

    @RequestMapping(value = "/queryModulMaintianId", method = RequestMethod.GET)
    @ResponseBody
    public R queryModulMaintianId(HttpServletRequest request) {
        String modulId = request.getParameter("id");
        List<Map<String, Object>> modulList = maintainPlanService.selectModulMaintian(modulId);
        for (Map map : modulList) {
            if (map != null) {
                return R.ok();
            }
        }
        return R.error(500, "维护库暂没有对应的数据，请添加");
    }


    @RequestMapping(value = "/upPlanReview", method = RequestMethod.POST)
    @ResponseBody
    public R upPlanReview(HttpServletRequest request) {
        String planId = request.getParameter("reviewId");
        String review = request.getParameter("reviewText");
        Integer reviewFlag = maintainPlanService.updatePlanReview(planId, review);
        if (reviewFlag > 0) {
            return R.ok();
        }
        return R.error(500, "评审失败");
    }


    @RequestMapping(value = "/queryKeppDept",method = RequestMethod.GET)
    @ResponseBody
    public R queryKeppDept(){
        List<Map<String,Object>> deptList = maintainPlanService.queryDeptPerson();
        return R.ok().put("deptList",deptList);
    }

    @RequestMapping(value = "/dispatchPlan",method = RequestMethod.POST)
    @ResponseBody
    public R dispatchPlan(MaintainPlanEntity maintainPlanEntity,HttpServletRequest request){
        String minorPerson = request.getParameter("minorPerson");
        JSONArray personJ = JSON.parseArray(minorPerson);
        Integer flag = maintainPlanService.updatePlan(maintainPlanEntity,personJ);
        if (flag > 0){
            return R.ok().put("msg","分派任务成功");
        }
        return R.error(500,"分派任务失败");
    }

    @RequestMapping(value = "/delPlanPerson",method = RequestMethod.POST)
    @ResponseBody
    public R delPlanPerson(HttpServletRequest request){
        String ids = request.getParameter("ids");
        JSONArray idsList = JSON.parseArray(ids);
        Integer flag = maintainPlanService.delPlanPerson(idsList);
        if (flag > 0){
            return R.ok().put("msg","删除成功");
        }
        return R.error(500,"删除失败");
    }

    @RequestMapping(value = "/allDevPlan",method = RequestMethod.GET)
    @ResponseBody
    public R allDevPlan(HttpServletRequest request){
        String id = request.getParameter("id");
        List<Map<String,Object>> allList = maintainPlanService.allDevPlan(id);
        return R.ok().put("allList",allList);
    }

    @RequestMapping(value = "/upStartDate",method = RequestMethod.POST)
    @ResponseBody
    public R upStartDate(HttpServletRequest request){
        String id = request.getParameter("id");
        Integer flag = maintainPlanService.upStartDate(id);
        if (flag > 0){
            return R.ok().put("msg","任务计划已开始执行");
        }
        return R.error(500,"任务计划执行失败");
    }

    @RequestMapping(value = "/insertRecord",method = RequestMethod.POST)
    @ResponseBody
    public R insertRecord(HttpServletRequest request){
        String rId = request.getParameter("rId");
        String pId = request.getParameter("pId");
        String carriedDate = request.getParameter("carriedDate");
        String carriedName = request.getParameter("carriedName");
        Integer flag = maintainPlanService.insertRecord(rId,pId,carriedDate,carriedName);
        if (flag > 0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping(value = "/insertPlanChange",method = RequestMethod.POST)
    @ResponseBody
    public R insertPlanChange(MaintainPlanChangeEntity changeEntity){
        Integer flag = maintainPlanService.insertPlanChange(changeEntity);
        if (flag > 0){
            return R.ok().put("msg","申请成功，请等待!");
        }
        return R.error(500,"申请失败，请重试!");
    }

    @RequestMapping(value = "/countPlanChange",method = RequestMethod.GET)
    @ResponseBody
    public R countPlanChange(HttpServletRequest request){
        String id = request.getParameter("id");
        Integer flag = maintainPlanService.countPlanChange(id);
        if (flag > 0){
            return R.ok().put("msg","已提交审批,请等待审批");
        }
        return R.ok();
    }

    @RequestMapping(value = "/reviewPlanChange",method = RequestMethod.GET)
    @ResponseBody
    public R reviewPlanChange(){
        List<MaintainPlanChangeEntity> allList = maintainPlanService.reviewPlanChange();
        return R.ok().put("allList",allList);
    }

    @RequestMapping(value = "/turnDownPlanChange",method = RequestMethod.POST)
    @ResponseBody
    public R turnDownPlanChange(MaintainPlanChangeEntity changeEntity){
        Integer flag = maintainPlanService.turnDownPlanChange(changeEntity);
        if (flag > 0){
            return R.ok().put("msg","驳回成功");
        }
        return R.error();
    }

    @RequestMapping(value = "/upExecutive",method = RequestMethod.POST)
    @ResponseBody
    public R upExecutive(HttpServletRequest request){
        String id = request.getParameter("id");
        String pid = request.getParameter("pid");
        Integer flag = maintainPlanService.upExecutive(id,pid);
        if (flag > 0){
            return R.ok().put("msg","审批已通过");
        }
        return R.error(500,"错误信息");
    }




    @RequestMapping(value = "/endPlan",method = RequestMethod.POST)
    @ResponseBody
    public R endPlan(HttpServletRequest request){
        String id = request.getParameter("id");
        String msg = maintainPlanService.endPlan(id);
        return R.ok().put("msg",msg);
    }


    /**
     * 修改
     */
    @RequestMapping("/apply")
    @RequiresPermissions("maintainplan:apply")
    @ResponseBody
    public R apply(@RequestBody MaintainPlanEntity maintainPlan) {
        maintainPlanService.apply(maintainPlan);

        return R.ok();
    }

    /**
     * 查看规程任务详情
     */
    @RequestMapping("/details")
    //@RequiresPermissions("maintainplan:details")
    @ResponseBody
    public R details(@RequestParam Map<String, Object> params) {
        //查询规程任务详情
        Query query = new Query(params);
        List<Map<String,Object>> maintainPlanEntity = maintainPlanService.getDetailsInfo(params);
        int total = maintainPlanService.queryDetailsTotal(query);
        PageUtils pageUtil = new PageUtils(maintainPlanEntity, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/infos/{id}")

    @ResponseBody
    public R infos(@PathVariable("id") String id) {
        MaintainPlanEntity maintainPlan = maintainPlanService.queryObject(id);

        return R.ok().put("maintainPlan", maintainPlan);
    }
}
